<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>广告运营</el-breadcrumb-item>
        <el-breadcrumb-item>广告位id设置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-del="rowDel"
      >
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            plain
            @click.stop="handleAdd()"
            >新增</el-button
          >
        </template>
        <!-- 自定义表单内容--图片显示 -->
        <template slot="ad_type" slot-scope="scope">
          <!-- {{scope.row.ad_type}} -->
          <span
            v-for="(item, value, index) in JSON.parse(scope.row.ad_type)"
            :key="index"
          >
            <p>
              {{item.name}}：↓
              <br> 
              （曝光次数：{{ item.exposure }}）
              <br>
              （第三方广告编号：{{item.ad_position_id}}）
            </p>
          </span>
        </template>
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <br />
          <el-button
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="editAD(scope)"
            >编辑</el-button
          >
          <br />
          <el-button
            v-if="scope.row.is_close == 1"
            icon="el-icon-open"
            type="text"
            size="small"
            @click.stop="openAndClose(scope.row, 0)"
            >开启广告
          </el-button>
          <el-button
            v-if="scope.row.is_close == 0"
            icon="el-icon-open"
            type="text"
            size="small"
            @click.stop="openAndClose(scope.row, 1)"
            >关闭广告
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>
<script>
import {
  getAdProvider,
  getAdPositionList,
  getAppConfigAdLists,
  closeAndOpenConfigAd,
  deleteAppConfigAd
} from "@/api/cat/ad";
import { catUrl } from "@/config/env";
import { selectVersion } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
      },
      type: true,
      data: [],
      form: {},
      option: {
        index: true,
        indexLabel: "序号",
        editBtn: false,
        viewBtn: false,
        searchBtn: true,
        refreshBtn: true,
        border: true,
        addBtn: false,
        delBtn: true,
        excelBtn: true,
        column: [
          {
            label: "广告位置位置名称",
            prop: "ad_position_name",
            type: "select",
            search: true,
            dicData: [],
            props: {
              label: "advertiser_type",
              value: "id",
            },
          },
          {
            label: "广告供应商",
            prop: "ad_type",
            type: "check",
            slot: true,
            width:'280px',
          },
          {
            label: "广告供应商",
            prop: "id",
            type: "check",
            hide: true,
            search: false
          },
            {
            label: "版本号",
            prop: "version_name",
            type: "select",
            search: true,
            placeholder: "请输入版本号",
            dicUrl: `${catUrl}/${selectVersion.selectVersion}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "广告位编号",
            prop: "ad_position_id",
            type: "select",
          },
          {
            type: "select",
            label: "保底广告供应商1",
            prop: "second_ad_name",
          },
          {
            type: "input",
            label: "保底广告位编号1",
            prop: "second_ad_position_id",
          },
          {
            label: "保底广告供应商2",
            prop: "third_ad_name",
            type: "select",
          },
          {
            label: "保底广告位编号2",
            prop: "third_ad_position_id",
            type: "input",
          },
          // {
          //   label: "广告位名称",
          //   prop: "title",
          //   type: "input",
          // },
          // {
          //   label: "广告位描述",
          //   prop: "description",
          //   type: "input",
          // },
          // {
          //   type: "upload",
          //   label: "活动图",
          //   span: 12,
          //   propsHttp: {},
          //   canvasOption: {},
          //   prop: "ad_image_url",
          //   listType: "picture-img",
          // },
          // {
          //   label: "跳转地址",
          //   type: "textarea",
          //   prop: "ad_image_jump",
          // },
          {
            label: "状态",
            prop: "is_close",
            type: "select",
            search: true,
            dicData: [
              {
                label: "开启",
                value: 0,
              },
              {
                label: "关闭",
                value: 1,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
        ],
      },
    };
  },
  async created() {
    let response = await getAdPositionList();
    this.option.column[0].dicData = response.data.source;
    //尝试获取第几页--获取到说明是修改完跳转回来的。
    let pageNum = this.$route.query.pageNum;
    console.info(pageNum)
    if(pageNum!=undefined){
      this.page.currentPage = pageNum;
    }
  },
  methods: {
    
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let {data} = await deleteAppConfigAd(row.id);
        })
        .then(() => {
          this.getList();
        });
    },
    /**修改开启和关闭配置 */
    async openAndClose(value, type) {
      let { data } = await closeAndOpenConfigAd(value.id, type);
       
      this.getList();
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //封装参数
      let param = Object.assign(
        {
          pageNum: this.page.currentPage,
          pageSize: this.page.pageSize,
        },
        this.params
      );
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await getAppConfigAdLists(param);
      //修改分页内容
      console.log(response)
      this.pages(response.data);
      //数据赋值
      let list = response.data.rows
      this.data = list;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList(); 
    },
    pages(data) {
      this.page.total = data.total;
      this.page.pageSize = data.size;
      this.page.currentPage = data.current;
    },
    searchChange(params, done) {
      this.condition = [];
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    editAD(scope) {
      this.$router.push("/org/ad/adidconfigedit?ad_id=" + scope.row.id+"&pageNum="+this.page.currentPage);
    },
    handleAdd(scope) {
      this.$router.push("/org/ad/adidconfigadd");
    },
  },
};
</script>